/* This is the panel for the battle gui
 * 
 */


//
package gui.battle;

import java.awt.*;
import java.util.*;

import item.ship.*;
import item.weapon.*;

import javax.swing.*;

import ai.AutoAttack;

public class BattlePanel extends JPanel{


	private static final long serialVersionUID = 3685606515475877555L;
	static BattleShip turn; //the ship whose turn it is
	static Weapon selectedWeapon; //the weapon currently selected
	static ShipInfoPanel sip;
	static PriorityQueue<BattleShip> order;
	static ArrayList<Ship> leftShips;
	static ArrayList<Ship> rightShips;
	private ShipBattlePanel sbp;
	private int numLeft;
	private int numRight;


	public BattlePanel(ArrayList<Ship> l, ArrayList<Ship> r){

		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setBackground(Color.GRAY);

		JPanel buffer = new JPanel(); //used as a buffer between panels
		buffer.setMaximumSize(new Dimension(1200 , 20));

		JPanel buffer2 = new JPanel(); //used as a buffer between panels
		buffer2.setOpaque(true);
		buffer2.setMaximumSize(new Dimension(1200 , 20));

		leftShips = l;
		rightShips = r;

		numLeft = leftShips.size();
		numRight = rightShips.size();

		order = new PriorityQueue<BattleShip>(l.size() + r.size(), BattleShip.speedComparator());

		for(Ship s: leftShips){

			order.add(new BattleShip(s));
		}//for

		for(Ship s: rightShips){

			order.add(new BattleShip(s));
		}//for


		turn = order.poll();
		turn.getShip().setActionPoints(turn.getShip().getMaxActionPoints());

		sbp = new ShipBattlePanel(leftShips, rightShips);
		sbp.select(turn.getShip());

		sip = new ShipInfoPanel(turn.getShip());

		this.add(new TopPanel(turn.getShip()));
		this.add(Box.createVerticalGlue());
		this.add(sbp);
		this.add(Box.createVerticalGlue());
		this.add(sip);	
		
		//THIS IS AI
		
		
		if(sbp.isOnRight(turn.getShip())){
			
			System.out.println("AI Turn");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			AutoAttack.auto(turn.getShip(), leftShips);
			endTurn();
		}//if		
	}//default constructor


	//ends the turn
	public void endTurn(){
		
		turn.getShip().regen();

		ArrayList<Ship> toKill = new ArrayList<Ship>();

		for(Ship s: leftShips)			
			if(s.getHull() == 0)
				toKill.add(s);

		for(Ship s: rightShips)
			if(s.getHull() == 0)
				toKill.add(s);

		for(Ship s: toKill)
			kill(s);


		if(numLeft == 0)
			System.out.println("You Lose!");

		if(numRight == 0)
			System.out.println("You Win!");		

		turn.setSpeed((.7 + ((((double)turn.getShip().getActionPoints()) / turn.getShip().getMaxActionPoints()) * .3)));
		sbp.deselect(turn.getShip());

		for(BattleShip b: order){

			b.addSpeed(50 * b.getShip().level);
		}//for

		order.add(turn);

		turn = order.poll();
		turn.getShip().setActionPoints(turn.getShip().getMaxActionPoints());

		sbp.select(turn.getShip());
		this.updateSIP(turn.getShip());
		

		//THIS IS AI
		
		
		if(sbp.isOnRight(turn.getShip())){
			
			System.out.println("AI Turn");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			AutoAttack.auto(turn.getShip(), leftShips);
			endTurn();
		}//if		
	}//endTurn


	//updates the bottom panel
	public void updateSIP(Ship s){

		this.remove(sip);
		sip = new ShipInfoPanel(s);
		this.add(sip);

		this.revalidate();
		this.repaint();
	}//updateSIP


	//kills ship passed in
	public void kill(Ship s){

		System.out.println("kill a ship");

		BattleShip b = new BattleShip(s);

		order.remove(b);

		leftShips.remove(s);
		rightShips.remove(s);

		if(sbp.isOnLeft(s))
			numLeft--;

		else
			numRight--;

		sbp.remove(s);
	}//kill
}//BattlePanel